<template>
  <view class="container">
    <view class="content-wrapper">
      <!-- 表单内容 -->
      <uv-form
        ref="formRef"
        :model="formData"
        :rules="formRules"
        labelPosition="left"
        labelWidth="160"
        borderBottom
      >
        <!-- 金额信息 -->
        <view class="form-card">
          <view class="card-title">
            <text>批量付款</text>
          </view>
          <uv-cell-group>
            <uv-form-item
              label="汇款金额"
              prop="paymentAmount"
              borderBottom
              required
            >
              <uv-input
                v-model="formData.paymentAmount"
                type="number"
                placeholder="请输入汇款金额"
                border="none"
                clearable
              >
                <template #prefix>
                  <text class="input-prefix">¥</text>
                </template>
              </uv-input>
            </uv-form-item>
          </uv-cell-group>
        </view>
      </uv-form>
    </view>

    <!-- 底部按钮 -->
    <view class="footer-buttons">
      <view class="button-group">
        <view class="button-wrapper">
          <uv-button
            type="primary"
            text="确认汇款"
            @click="handleSubmit"
            :loading="isSubmitting"
            :customStyle="{
              borderRadius: '12rpx',
            }"
          ></uv-button>
        </view>
      </view>
    </view>

    <!-- 提示组件 -->
    <uv-toast ref="toast"></uv-toast>
    <uv-notify ref="notify"></uv-notify>
  </view>
</template>

<script setup>
import { ref } from "vue";
import cloudConfig from "@/config/uniCloudConfig.js";
const uniCloudInStockManager = cloudConfig.getCloudObject("inStockManager");
// 组件引用
const formRef = ref(null);
const toast = ref(null);
const notify = ref(null);

// 状态管理
const isSubmitting = ref(false);

// 表单数据
const formData = ref({
  paymentAmount: "",
});

// 表单验证规则
const formRules = {
  paymentAmount: [
    {
      required: true,
      message: "请输入汇款金额",
      trigger: ["blur", "change"],
    },
    {
      validator: (rule, value, callback) => {
        if (value && (!isNumber(value) || Number(value) <= 0)) {
          callback(new Error("请输入有效的金额"));
        } else {
          callback();
        }
      },
      trigger: ["blur", "change"],
    },
  ],
};

// 数字验证
const isNumber = (value) => {
  return !isNaN(parseFloat(value)) && isFinite(value);
};

// 提交处理
const handleSubmit = async () => {
  if (isSubmitting.value) return;

  try {
    // 表单验证
    await formRef.value?.validate();

    isSubmitting.value = true;
    uni.showLoading({
      title: "提交中...",
      mask: true,
    });
    const userInfo = uni.getStorageSync("userInfo");
    let res = await uniCloudInStockManager.inStockPayment({
      userId: userInfo._id,
      paymentAmount: formData.value.paymentAmount,
    });
    console.log(res);
    if (res.code === 1) {
      notify.value?.show({
        type: "error",
        message: res.message || "付款失败",
      });
    } else {
      notify.value?.show({
        type: "success",
        message: "付款成功",
      });
    }

    // 重置表单
    formData.value.paymentAmount = "";
  } catch (error) {
    console.error("Submit error:", error);
    toast.value?.show({
      type: "error",
      message: error.message || "付款失败",
    });
  } finally {
    uni.hideLoading();
    isSubmitting.value = false;
  }
};
</script>

<style lang="scss" scoped>
.container {
  height: 100vh;
  background-color: #f8f8f8;
  position: relative;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.content-wrapper {
  flex: 1;
  padding: 20rpx;
  overflow-y: auto;
  padding-bottom: calc(140rpx + env(safe-area-inset-bottom));
}

.input-prefix {
  font-size: 28rpx;
  color: #666;
  padding: 0 10rpx;
}

.footer-buttons {
  padding: 30rpx 20rpx calc(30rpx + env(safe-area-inset-bottom));
  box-sizing: border-box;
  width: 100%;
  background-color: #fff;
  position: fixed;
  bottom: 0;
  left: 0;
  z-index: 99;
  box-shadow: 0 -2rpx 20rpx rgba(0, 0, 0, 0.05);
  display: flex;

  .button-group {
    display: flex;
    align-items: center;
    justify-content: space-between;
    max-width: 900rpx;
    margin: 0 auto;
    flex: 1;
    .button-wrapper {
      flex: 1;
      :deep(.uv-button-wrapper) {
        width: 100%;
      }
    }
  }
}

.form-card {
  background: #fff;
  border-radius: 12rpx;
  margin-bottom: 20rpx;
  padding: 24rpx;
  box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.04);

  .card-title {
    display: flex;
    align-items: center;
    margin: -24rpx -24rpx 20rpx;
    padding: 24rpx;
    background: linear-gradient(
      to right,
      rgba(41, 121, 255, 0.05),
      rgba(41, 121, 255, 0)
    );
    position: relative;

    &::before {
      content: "";
      position: absolute;
      left: 0;
      top: 50%;
      transform: translateY(-50%);
      width: 6rpx;
      height: 32rpx;
      background: #2979ff;
      border-radius: 0 4rpx 4rpx 0;
    }

    .icon-wrapper {
      width: 64rpx;
      height: 64rpx;
      border-radius: 16rpx;
      background: rgba(41, 121, 255, 0.1);
      display: flex;
      align-items: center;
      justify-content: center;
      margin-left: 16rpx;
    }

    text {
      font-size: 32rpx;
      font-weight: 600;
      color: #333;
      margin-left: 16rpx;
      letter-spacing: 1rpx;
    }
  }
}
</style>
